﻿
using System.IO;

using OpenCvSharp;

using Xc.DataAccess.CodeReader.Properties;
using Xc.DataAccess.Core;

namespace Xc.DataAccess.CodeReader.Models.Filters
{
    [Filter("视频记录", FilterType.OS)]
    public class OsVideoWriter : FilterBase
    {
        [InputParameter("API类型")]
        public VideoCaptureAPIs VideoCaptureAPIs { get; set; } = VideoCaptureAPIs.FFMPEG;
        [InputParameter("目标文件名")]
        public string FileName { get; set; } = System.IO.Path.Combine(Directory.GetCurrentDirectory(),"Video.mp4");
        [InputParameter("目标格式")]
        public FourCC FourCC { get; set; } = FourCC.H264;
        [InputParameter("帧数")]
        public double Fps { get; set; } = 30;

        [InputParameter("是否彩色")]
        public bool IsColor { get; set; } = true;
        VideoWriter writer;
        internal override ResultData<Mat> UseChannel(Mat mat, FilterBase[] filterInfos, FilterBase preFilterBase)
        {
            if (mat.Height==0)
            {

                return ResultData<Mat>.Reject(TextResource.detect_failed, mat);

            }
            if (IsColor)
            {
                mat.ConvertTo(mat, MatType.CV_8UC3);

            }
            else
            {
                mat.ConvertTo(mat, MatType.CV_8U);
            }
            if (writer==null||writer.IsDisposed)
            {
               var size=  mat.Size();
                writer = new VideoWriter(FileName, VideoCaptureAPIs, FourCC, Fps, size, IsColor);
                writer.Open(FileName, VideoCaptureAPIs, FourCC, Fps, size, IsColor);

            }
            writer.Write(mat);
           writer.Release();
            return ResultData<Mat>.Accept(TextResource.filter_action_complete, mat);

        }
    }

}
